<?php

namespace App\Model;

use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Model\SaRole.
 *
 * @property int         $id
 * @property string      $name       名称
 * @property int         $status     状态：0禁用 1启用
 * @property int         $is_admin   是否是超级管理员：0否 1是
 * @property int         $created_by 创建人id
 * @property Carbon|null $created_at 创建时间
 * @property Carbon|null $updated_at 更新时间
 * @property Carbon|null $deleted_at 删除时间
 *
 * @method static Builder|SaRole newModelQuery()
 * @method static Builder|SaRole newQuery()
 * @method static \Illuminate\Database\Query\Builder|SaRole onlyTrashed()
 * @method static Builder|SaRole query()
 * @method static Builder|SaRole whereCreatedAt($value)
 * @method static Builder|SaRole whereCreatedBy($value)
 * @method static Builder|SaRole whereDeletedAt($value)
 * @method static Builder|SaRole whereId($value)
 * @method static Builder|SaRole whereIsAdmin($value)
 * @method static Builder|SaRole whereName($value)
 * @method static Builder|SaRole whereStatus($value)
 * @method static Builder|SaRole whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|SaRole withTrashed()
 * @method static \Illuminate\Database\Query\Builder|SaRole withoutTrashed()
 * @mixin \Eloquent
 */
class SaRole extends BaseModel
{
    use SoftDeletes;
    protected $table = 'sa_role';

    /**
     * 添加角色.
     *
     * @param $info
     *
     * @return mixed
     */
    public static function addRole($info)
    {
        return self::insertGetId($info);
    }

    /**
     * 获取角色信息.
     *
     * @param $param
     *
     * @return array
     */
    public static function getInfo($param)
    {
        $where = [];
        if (array_key_exists('id', $param) && !empty($param['id'])) {
            $where[] = ['id', '=', $param['id']];
        }
        if (array_key_exists('name', $param) && !empty($param['name'])) {
            $where[] = ['name', '=', $param['name']];
        }

        $field = ['id', 'name', 'status', 'is_admin'];
        $info  = self::where($where)->select($field)->first();
        if ($info) {
            return $info->toArray();
        }

        return [];
    }

    /**
     * 获取角色列表.
     *
     * @param $param
     * @param $limit
     *
     * @return mixed
     */
    public static function getRoleList($param, $limit)
    {
        $where = [];
        if (array_key_exists('is_admin', $param) && null != $param['is_admin']) {
            $where[] = ['is_admin', '=', $param['is_admin']];
        }

        $field = ['id', 'name', 'status', 'is_admin', 'created_at'];

        return self::where($where)
                ->select($field)
                ->orderBy('is_admin', 'desc')
                ->paginate($limit)
                ->toArray();
    }

    /**
     * 编辑角色.
     *
     * @param $id
     * @param $info
     *
     * @return mixed
     */
    public static function editRole($id, $info)
    {
        return self::where(['id' => $id])->update($info);
    }

    /**
     * 删除角色.
     *
     * @param $id
     *
     * @return mixed
     */
    public static function delRole($id)
    {
        return self::where(['id' => $id])->delete();
    }
}
